﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using VanTaiBLL;
using VanTaiData;
using VanTaiProperty;
using Roles;
using System.Web.UI.HtmlControls;

namespace VanTaiTMS.SubPage
{
    public partial class uc_don_vi_tinh_xe : System.Web.UI.UserControl
    {
        #region[Define]
        private DataTable dtBlockVehicle;
        private DataTable dtUnitVehicle;
        protected string SuggestionList;
        protected string StrAlert = "";
        private clsTMUnit_VehicleBusiness clsUnitVehicle = new clsTMUnit_VehicleBusiness();
        private clsTMBock_VehicleBusiness clsBockVehicle = new clsTMBock_VehicleBusiness();
        #endregion

        #region[Controller]
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGridBlockVehicle();
            }
        }

        //---------------button-popup---------------------
        protected void imgrefesh_Click(object sender, ImageClickEventArgs e)
        {

        }
        protected void imgSave_Click(object sender, ImageClickEventArgs e)
        {
            if (txtMaDonViTinh.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào mã đơn vị tính !";
                OpenDialog("#ucthongbao");
                updatePanelThongBao.Update();
            }
            if (txtTenDonViTinh.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào tên đơn vị tính !";
                OpenDialog("#ucthongbao");
                updatePanelThongBao.Update();
            }
            if (drKhoi.SelectedValue.ToString() == "-1")
            {
                lbThongBaoLoi.Text = "Vui lòng chọn khối !";
                OpenDialog("#ucthongbao");
                updatePanelThongBao.Update();
            }
            if (txtTrongTai.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào trọng tải !";
                OpenDialog("#ucthongbao");
                updatePanelThongBao.Update();
            }
            if (txtMaDonViTinh.Text != "" && txtTenDonViTinh.Text != "" && txtTrongTai.Text != "" && drKhoi.SelectedValue.ToString() != "-1")
            {
                InsertUnitVehicle();
            }
        }
        protected void cmdClose_Click(object sender, EventArgs e)
        {
            CloseDialog("#ucthongbaoxoa");
        }
        protected void cmdCloseThongBao_Click(object sender, EventArgs e)
        {
            CloseDialog("#ucthongbao");
        }
        protected void ImgRefeshDuyet_Click(object sender, ImageClickEventArgs e)
        {

        }
        protected void ImgRefeshExport_Click(object sender, ImageClickEventArgs e)
        {
            //txtTuNgayNhapKhoExport.Text = "";
            //txtDenNgayNhapKhoExport.Text = "";
            //BindGridLenhDieuDongExportExcel();
        }
        //---------Dropdownlist-----------
        protected void drTrongTai_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        #endregion

        #region[Function]
        protected void BindGridBlockVehicle()
        {
            dtBlockVehicle = clsBockVehicle.BindGirdTMBock_Vehicle();
            if (dtBlockVehicle != null && dtBlockVehicle.Rows.Count > 0)
            {
                drKhoi.DataSource = dtBlockVehicle;
                drKhoi.DataTextField = "Block_Vehicel_Alias";
                drKhoi.DataValueField = "Block_Vehicle_ID";
                drKhoi.DataBind();
            }
        }
        protected bool CheckInsert()
        {
            bool kiemtra = false;
            dtUnitVehicle = clsUnitVehicle.BindGirdTMUnit_Vehicle();
            if (dtUnitVehicle != null && dtUnitVehicle.Rows.Count > 0)
            {
                foreach (DataRow dr in dtUnitVehicle.Rows)
                {
                    if (txtMaDonViTinh.Text == dr["Unit_Vehicle_Alias"].ToString())
                    {
                        kiemtra = true;
                    }
                }
            }
            return kiemtra;
        }
        protected void InsertUnitVehicle()
        {
            try
            {
                if (CheckInsert())
                {
                    lbThongBaoLoi.Text = "Mã đơn vị tính này đã tồn tại !";
                    OpenDialog("#ucthongbao");
                    updatePanelThongBao.Update();
                }
                else
                {
                    VanTaiProperty.clsTMUnit_VehicleProperty obj = new clsTMUnit_VehicleProperty();
                    obj.Unit_Vehicle_Alias = txtMaDonViTinh.Text;
                    obj.Unit_Vehicle_Name = txtTenDonViTinh.Text;
                    obj.Unit_Vehicle_Number = float.Parse(txtTrongTai.Text.Replace(",", ""));
                    obj.Unit_Block_Vehicle_ID = int.Parse(drKhoi.SelectedValue.ToString());
                    VanTaiBLL.clsTMUnit_VehicleBusiness.InsertTMUnit_Vehicle(obj);
                    lbThongBaoLoi.Text = "Thêm mẫu tin thành công !";
                    OpenDialog("#ucthongbao");
                    updatePanelThongBao.Update();
                    ClearTextBox();
                    ClearDropdownlist();
                }
            }
            catch (Exception ex)
            {
                lbThongBaoLoi.Text = ex.ToString();
                OpenDialog("#ucthongbao");
                updatePanelThongBao.Update();
            }
        }
        #endregion

        #region[OpenDialog]
        private void OpenDialog(string dialogId)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('open');");
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
               "scriptKey", sb.ToString(), true);
        }
        private void CloseDialog(string dialogId)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('close');");
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
               "scriptKey", sb.ToString(), true);
        }
        #endregion

        #region[Clear-TextBox]
        protected void ClearTextBox()
        {
            txtMaDonViTinh.Text = "";
            txtTenDonViTinh.Text = "";
            txtTrongTai.Text = "0";
        }
        protected void ClearDropdownlist()
        {
            drKhoi.SelectedIndex = -1;
        }
        #endregion
    }
}